package com.baiye.template.controller;

import com.baiye.template.common.R;
import com.baiye.template.pojo.MyFile;
import com.baiye.template.service.FileService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/9 11:08
 */
@RestController
@Api(value = "文件管理")
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    @RequestMapping(value = "/uploadFileToOSS",method = RequestMethod.POST)
    public R uploadFileToOSS(@RequestParam("file")MultipartFile file){
        fileService.uploadFileToOSS(file);
        return new R().ok();
    }
    @RequestMapping(value = "/uploadFileToLocal/{userId}",method = RequestMethod.POST)
    public R uploadFileToLocal(@RequestParam("file")MultipartFile file,@PathVariable("userId") Integer userId){
        String path=fileService.uploadFileToLocal(file,userId);
        return new R().ok(path);
    }
    @RequestMapping(value = "/getFile",method = RequestMethod.GET)
    public R getFile(@Param("file") MyFile myFile, @Param(value = "pageIndex")Integer pageIndex, @Param(value = "pageSize") Integer pageSize){
        List<MyFile> myFileList =fileService.getFile(myFile,pageIndex,pageSize);
        PageInfo<MyFile> pageInfo = new PageInfo<>(myFileList);
        return new R().ok(pageInfo);
    }
}
